"""This code runs a report about OSI and downloads it as a csv file.
Re-formatting is performed from US to rest-of-the-world formatting.
The report is then shared with the publisher via email.
It is meant to be scheduled to run daily each morning"""

__author__ = 'gspillmann@google.com (Guillaume Spillmann)'

import csv
import os
import smtplib
import sys
import time

from adspygoogle.dfp import DfpUtils
from adspygoogle.dfp.DfpClient import DfpClient
from email import Encoders
from email.MIMEBase import MIMEBase
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate

sys.path.insert(0, os.path.join('..', '..', '..', '..'))

# Initialize client object.
client = DfpClient(path=os.path.join('..', '..', '..', '..'))

# Initialize appropriate service. By default, the request is always made against
# sandbox environment.
report_service = client.GetReportService(
    'https://www.google.com', 'v201203')

def send_mail(csv, server="localhost"):

  recipients = ['gspillmann@google.com']
  msg = MIMEMultipart()
  msg['From'] = 'gspillmann@google.com'
  msg['To'] = ','.join(recipients)
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = 'Rapport sur les campagnes en retard'
  msg.attach( MIMEText('Rapport quotidien avec volumes de la veille et \
                        indicateur de diffusion'))
  part = MIMEBase('application', "octet-stream")
  part.set_payload( open(csv,"rb").read() )
  Encoders.encode_base64(part)
  part.add_header('Content-Disposition', 'attachment; filename="%s"' \
                  % os.path.basename(csv))
  msg.attach(part)

  smtp = smtplib.SMTP(server)
  smtp.sendmail(msg['From'], msg['To'], msg.as_string())
  smtp.close()

# Create report job.
report_job = {
    'reportQuery': {
        'dimensions': ['LINE_ITEM_TYPE', 'ADVERTISER', 'LINE_ITEM',
                       'CREATIVE_SIZE', 'CUSTOM_TARGETING', 'AD_UNIT'],
        'columns': ['AD_SERVER_IMPRESSIONS','AD_SERVER_DELIVERY_INDICATOR'],
        'dateRangeType': 'YESTERDAY',
        'adUnitView': 'TOP_LEVEL',
    }
}

query = report_job['reportQuery']
columns = query['columns']

# Run report.
report_job = report_service.RunReportJob(report_job)[0]

# Wait for report to complete.
status = report_job['reportJobStatus']
while status != 'COMPLETED' and status != 'FAILED':
  time.sleep(30)
  status = report_service.GetReportJob(report_job['id'])[0]['reportJobStatus']

  # Change to your preffered export format.
  export_format = 'CSV'

  # Download report data.
  data = DfpUtils.DownloadReport(report_job['id'],
                                 export_format,
                                 report_service)

  # Write to  file
  f = open('dfp_report_' + report_job['id'] + '.csv','w')
  f.write(data)
  f.close()

  # Substitute characters to make it Excel-ready
  row = 0
  f = csv.reader(open('dfp_report_' + report_job['id'] + '.csv','rb'),
                 delimiter=',', quotechar='"')
  r = open('dfp_report_' + report_job['id'] + '_final.csv','w')
  titles = f.next()
  
  # Removing last column for parent IU volume, irrelevant here
  columnsNumber = len(titles)
  for title in titles[:columnsNumber - 1]:
    r.write(str(title) + ';')
  r.write('\n')
  
  # Decimals from US format to rest-of-the-world format   
  for data in f:
    row +=1
    for item in data[:columnsNumber - 1]:
      item = str(item).replace(',', '')
      item = str(item).replace('.', ',')
      item = str(item).replace('\'', '')
      item = str(item).replace('\xe2\x82\xac', '')
      r.write(str(item) + ';')
    r.write('\n')
  r.close()
  
log = open('success.log','a')
m = 'Underdelivery report id %s downladed, %s\n' % (report_job['id'],query)
log.write(m)
log.close()

send_mail('dfp_report_' + report_job['id'] + '_final.csv')